home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 59 / 59.xpi / chrome / useragentswitcher.jar / content / useragentswitcher / xml / export.js next >
Text File  |  2009-06-30  |  5KB  |  187 lines

  1. // User Agent Switcher exporter
  2. var UserAgentSwitcherExporter =
  3. {
  4.     // Exports the user agents to a file
  5.     export: function(file)
  6.     {
  7.         // Try to export to a file
  8.         try
  9.         {
  10.             // If the file does not exist
  11.             if(!file.exists())
  12.             {
  13.                 file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 00644);
  14.             }
  15.     
  16.             // If the file exists, is a file and is writable
  17.             if(file.exists() && file.isFile() && file.isWritable())
  18.             {
  19.                 var element       = null;
  20.                 var outputStream  = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
  21.                 var xmlDocument   = document.implementation.createDocument("", "", null);
  22.                 var rootElement   = xmlDocument.createElement("useragentswitcher");
  23.                 var xmlSerializer = new XMLSerializer();
  24.     
  25.                 this.exportFile(document.getElementById("useragentswitcher-options-user-agents").childNodes, rootElement, xmlDocument);
  26.     
  27.                 xmlDocument.appendChild(rootElement);
  28.  
  29.                 outputStream.init(file, 0x04 | 0x08 | 0x20, 00644, null);
  30.                 xmlSerializer.serializeToStream(xmlDocument, outputStream, "utf-8");
  31.                 outputStream.close();
  32.             }
  33.             else
  34.             {
  35.                 return UserAgentSwitcherStringBundle.getFormattedString("exportFileFailed", [file.path]);
  36.             }
  37.         }
  38.         catch(exception)
  39.         {
  40.             UserAgentSwitcherLog.log("UserAgentSwitcherExporter.export", exception);
  41.         }
  42.         
  43.         return null;
  44.     },
  45.  
  46.     // Exports the user agents
  47.     exportFile: function(elements, parentElement, xmlDocument)
  48.     {
  49.         var elementsLength = elements.length;
  50.  
  51.         // Loop through the elements
  52.         for(var i = 0; i < elementsLength; i++)
  53.         {
  54.             var element = elements[i];
  55.             
  56.             // If the element is a separator
  57.             if(element.nodeName == "treeseparator")
  58.             {
  59.                 this.exportSeparator(parentElement, xmlDocument);
  60.             }
  61.             else if(element.hasAttribute("container"))
  62.             {
  63.                 this.exportFolder(element, parentElement, xmlDocument);
  64.             }
  65.             else
  66.             {
  67.                 this.exportUserAgent(element, parentElement, xmlDocument);
  68.             }
  69.         }
  70.     },
  71.     
  72.     // Exports a folder
  73.     exportFolder: function(element, parentElement, xmlDocument)
  74.     {
  75.         var folder        = xmlDocument.createElement("folder");
  76.         var folderElement = element.getElementsByTagName("treerow")[0].getElementsByTagName("treecell")[0];
  77.  
  78.         // If the folder has a label
  79.         if(folderElement.hasAttribute("label"))
  80.         {
  81.             folder.setAttribute("description", folderElement.getAttribute("label"));
  82.         }
  83.  
  84.         this.exportFile(element.getElementsByTagName("treechildren")[0].childNodes, folder, xmlDocument);
  85.     
  86.         parentElement.appendChild(folder);
  87.     },
  88.  
  89.     // Exports a separator
  90.     exportSeparator: function(parentElement, xmlDocument)
  91.     {
  92.         parentElement.appendChild(xmlDocument.createElement("separator"));
  93.     },
  94.     
  95.     // Exports a user agent
  96.     exportUserAgent: function(element, parentElement, xmlDocument)
  97.     {
  98.         var userAgentElement = element.getElementsByTagName("treerow")[0].getElementsByTagName("treecell")[0];
  99.         
  100.         // If the user agent has a label
  101.         if(userAgentElement.hasAttribute("label"))
  102.         {
  103.             var userAgent = xmlDocument.createElement("useragent");
  104.  
  105.             userAgent.setAttribute("description", userAgentElement.getAttribute("label"));
  106.  
  107.             // If the user agent is set
  108.             if(userAgentElement.hasAttribute("useragentswitcheruseragent"))
  109.             {
  110.                 userAgent.setAttribute("useragent", userAgentElement.getAttribute("useragentswitcheruseragent"));
  111.             }
  112.             else
  113.             {
  114.                 userAgent.setAttribute("useragent", "");
  115.             }
  116.     
  117.             // If the app code name is set
  118.             if(userAgentElement.hasAttribute("useragentswitcherappcodename"))
  119.             {
  120.                 userAgent.setAttribute("appcodename", userAgentElement.getAttribute("useragentswitcherappcodename"));
  121.             }
  122.             else
  123.             {
  124.                 userAgent.setAttribute("appcodename", "");
  125.             }
  126.     
  127.             // If the app name is set
  128.             if(userAgentElement.hasAttribute("useragentswitcherappname"))
  129.             {
  130.                 userAgent.setAttribute("appname", userAgentElement.getAttribute("useragentswitcherappname"));
  131.             }
  132.             else
  133.             {
  134.                 userAgent.setAttribute("appname", "");
  135.             }
  136.     
  137.             // If the app version is set
  138.             if(userAgentElement.hasAttribute("useragentswitcherappversion"))
  139.             {
  140.                 userAgent.setAttribute("appversion", userAgentElement.getAttribute("useragentswitcherappversion"));
  141.             }
  142.             else
  143.             {
  144.                 userAgent.setAttribute("appversion", "");
  145.             }
  146.     
  147.             // If the platform is set
  148.             if(userAgentElement.hasAttribute("useragentswitcherplatform"))
  149.             {
  150.                 userAgent.setAttribute("platform", userAgentElement.getAttribute("useragentswitcherplatform"));
  151.             }
  152.             else
  153.             {
  154.                 userAgent.setAttribute("platform", "");
  155.             }
  156.     
  157.             // If the vendor is set
  158.             if(userAgentElement.hasAttribute("useragentswitchervendor"))
  159.             {
  160.                 userAgent.setAttribute("vendor", userAgentElement.getAttribute("useragentswitchervendor"));
  161.             }
  162.             else
  163.             {
  164.                 userAgent.setAttribute("vendor", "");
  165.             }
  166.     
  167.             // If the vendor sub is set
  168.             if(userAgentElement.hasAttribute("useragentswitchervendorsub"))
  169.             {
  170.                 userAgent.setAttribute("vendorsub", userAgentElement.getAttribute("useragentswitchervendorsub"));
  171.             }
  172.             else
  173.             {
  174.                 userAgent.setAttribute("vendorsub", "");
  175.             }
  176.     
  177.             parentElement.appendChild(userAgent);
  178.         }
  179.     },
  180.  
  181.     // Returns the user agent file location
  182.     getUserAgentFileLocation: function()
  183.     {
  184.         return UserAgentSwitcherImporter.getUserAgentFileLocation();
  185.     }
  186. };
  187.